package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * 清理垃圾图片
 * Author chongge
 * Time 2019/10/27 19:35
 * ClassName com.itheima.jobs
 */
public class ClearImage {


    @Autowired
    private JedisPool jedisPool;

    public void deleteImage() {

        //获取redis中两个集合的sdiff:差集
        Set<String> sdiff = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (sdiff != null && sdiff.size() > 0) {
            for (String imgName : sdiff) {

                //删除七牛云图片
                QiniuUtils.deleteFileFromQiniu(imgName);
                //清空redis缓存
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, imgName);
                System.out.println("删除垃圾图片成功" + imgName);
            }
        }

    }
}
